package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.vo.resp.DeviceDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.domain.DeviceInfo;
import com.task.core.dto.request.DeviceRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.DeviceService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:30
 */
@Api("")
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {


  @Resource
  private DeviceService deviceService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('device:list')")
  @GetMapping("/getDeviceByPage")
  public TableDataInfo getDeviceByPage(DeviceRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(deviceService.getDeviceByPage(request));
  }


  @GetMapping("/getChildDeptByAdminCreate")
  public AjaxResult getChildDeptByAdminCreate() {
    return success(deviceService.getChildDeptByAdminCreate());
  }


  @PreAuthorize("@ss.hasPermi('device:option')")
  @GetMapping("/getDeviceForOption")
  public TableDataInfo getDeviceForOption(DeviceRequestDTO request) {
    return getDataTable(deviceService.getDeviceForOption(request));
  }

  @Log(title = "设备管理", businessType = BusinessType.INSERT)
  @PostMapping("/addDevice")
  public AjaxResult addDevice(@RequestBody DeviceRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());

    return success(deviceService.addDevice(request));
  }

  @Log(title = "设备管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyDevice")
  public AjaxResult modifyDevice(@RequestBody DeviceRequestDTO request) {
    checkService.check("data_device", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    deviceService.modifyDevice(request);
    return success();
  }

  @Log(title = "设备管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteDevice/{DeviceCode}")
  public AjaxResult deleteDevice(@PathVariable String DeviceCode) {
    checkService.check("data_device", DeviceCode);
    deviceService.deleteDevice(DeviceCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }


  @GetMapping("/getDeviceDetailByCode/{deviceCode}")
  @PreAuthorize("@ss.hasPermi('system:device:edit')")
  public AjaxResult getDeviceDetailByCode(@PathVariable String deviceCode) {
    checkService.check("data_device", deviceCode);
    DeviceDetailRespVo deviceDetail = deviceService.getDeviceDetailByCode(deviceCode);
    return success(deviceDetail);
  }

}
